<?php
/**
 * 成交查询
 */
namespace app\data\controller;
use \think\Controller;
use \think\Db;

class Deal extends Controller
{
    public function index()
    {
        //查询系统内置用户表用于比对
        $sys_user=db('add_name')->field('account,id')->select();

        //url
        $url_user=LOGIN_HOST.'/trader/proxy/organize/query/orders/match/page';
        $url_user_data = array (
            'search_LIKE_sysRelationUserOrg.userSecurity.loginName' => '',
            'search_LLIKE_sysRelationUserOrg.organizeRelation.orgCode' => '',
            'search_EQ_productId' => '',
            'search_EQ_direct' => '',
            'search_EQ_matchWay' => '',
            'page'=>'1',
            'limit'=>'10000'
        );

        $content = GetPost($url_user, COOKIE_PATH,$url_user_data);
        //过滤采集数据
        $data=json_decode($content);

        //清空客户表准备重新写入信息
        if($data){
            $delete_sql="truncate table ke_deal";
            Db::execute($delete_sql);
        }else{
            sendMail(['6325610@qq.com'=>'可是','381508990@qq.com'=>'波仔'],'采集失败-客户成交查询表',$content);
            return ;
        }
        //输出内容
        $res=$data->results->content;

        $arr=array();
        //批量写入数据库
        foreach ($res as $k => $it){
                //交易账号
                $arr[$k]['account']=$it->clientNo;
                //用户名
                $arr[$k]['user']=$it->userName;
                //单号
                $arr[$k]['order_num']=$it->matchNo;
                //商品名称
                $arr[$k]['goods']=$it->productName;
                //买卖类型
                $arr[$k]['type']=$it->direct;
                //成交价格
                $arr[$k]['deal_price']=$it->matchPriceRmb;
                //成交量
                $arr[$k]['deal_num']=$it->matchVol;
                //手续费
                $arr[$k]['charge']=$it->matchFeeRmb;
                //成交时间
                $arr[$k]['time']=substr($it->matchDateTime,0,10);

                foreach ($sys_user as $sys){
                    if($arr[$k]['account'] == $sys['account'] ){
                        $arr[$k]['pid'] = $sys['id'];
                        break;
                    }
                }


        }
        db("deal")->insertAll($arr);
        sendMail(['6325610@qq.com'=>'可是','381508990@qq.com'=>'波仔'],'客户成交查询表采集成功',$content);

    }

}
